package week02

// 34. 在排序数组中查找元素的第一个和最后一个位置
// https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/

func searchRange(nums []int, target int) []int {
	var ans = make([]int, 2)
	var left, right = 0, len(nums)
	for left < right {
		var mid = (right - left) >> 1 + left
		if nums[mid] >= target {
			right = mid
		} else {
			left = mid + 1
		}
	}
	ans[0] = left
	left, right = -1, len(nums)-1
	for left < right {
		var mid = (right - left + 1) >> 1 + left
		if nums[mid] <= target {
			left = mid
		} else {
			right = mid - 1
		}
	}
	ans[1] = left
	if ans[0] > ans[1] {
		ans[0], ans[1] = -1, -1
	}
	return ans
}
